package com.utils.annotationWork;

import com.config.globalException.BusinessException;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * <P>
 *     注解校验器
 * </P>
 * @since 2022年1月20日21:43:01
 * @author clay
 * @version 1.0
 */
public class NotEmptyValidated implements ConstraintValidator<com.utils.annotation.NotEmpty, Object> {

    private String message;
    private boolean isRequire;

    @Override
    public void initialize(com.utils.annotation.NotEmpty constraintAnnotation) {
        this.message = constraintAnnotation.message();
        this.isRequire = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(Object s, ConstraintValidatorContext constraintValidatorContext) {

        if(isRequire) {
            System.out.println("??");
            System.out.println(s);
            System.out.println(message);
            System.out.println(isRequire);
            if (s == null || StringUtils.isEmpty(String.valueOf(s))) {
                System.out.println("为啥呢");
                throw new BusinessException(message);
            }
        }
        return true;
    }
}
